#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.67])
AC_INIT([mooigraph], [1.7], [mooigraph@gmail.com],[mooigraph.tar.gz],[http://mooigraph.sourceforge.net/])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])

AC_CANONICAL_BUILD
AC_CANONICAL_HOST
AC_CANONICAL_TARGET

dnl optional gcc option can be like this AM_INIT_AUTOMAKE([-Wall])
AM_INIT_AUTOMAKE

AC_CONFIG_MACRO_DIR([m4])

AC_USE_SYSTEM_EXTENSIONS

dnl needed to support older automake versions
AC_SUBST(abs_top_builddir)
AC_SUBST(abs_top_srcdir)

# Checks for programs.
AC_PROG_AWK
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AM_PROG_CC_C_O

# Gcc compiler options
AC_DEFUN([MOOIGRAPH_WARNING],
[AC_MSG_CHECKING(whether compiler accepts $1)
AC_SUBST(WARNING_CFLAGS)
ac_save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $1"
AC_TRY_COMPILE(,
[int x;],
WARNING_CFLAGS="$WARNING_CFLAGS $1"
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
CFLAGS="$ac_save_CFLAGS"])

AC_ARG_ENABLE(gcc-warnings,
              AC_HELP_STRING([--enable-gcc-warnings],
	                     [turn on lots of GCC warnings (not recommended)]),
[case "${enableval}" in
   yes|no) ;;
   *)      AC_MSG_ERROR([bad value ${enableval} for gcc-warnings option]) ;;
 esac],
              [enableval=no])
if test "${enableval}" = yes; then
  MOOIGRAPH_WARNING(-Werror)
  AC_SUBST([WERROR_CFLAGS], [$WARNING_CFLAGS])
  WARNING_CFLAGS=
  MOOIGRAPH_WARNING(-Wall)
  MOOIGRAPH_WARNING(-W)
  MOOIGRAPH_WARNING(-Wbad-function-cast)
  MOOIGRAPH_WARNING(-Wcast-align)
  MOOIGRAPH_WARNING(-ftrapv)
  MOOIGRAPH_WARNING(-Wformat)
  MOOIGRAPH_WARNING(-Wmissing-declarations)
  MOOIGRAPH_WARNING(-Wmissing-prototypes)
  MOOIGRAPH_WARNING(-Wnested-externs)
  MOOIGRAPH_WARNING(-Wshadow)
  MOOIGRAPH_WARNING(-Wstrict-prototypes)
  MOOIGRAPH_WARNING(-Wwrite-strings)
else
  WARNING_CFLAGS=
  MOOIGRAPH_WARNING(-Wall)
fi


# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([fcntl.h inttypes.h libintl.h stddef.h stdint.h stdlib.h string.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

# math lib
AC_CHECK_LIB(m, sqrt,, echo "Math library -lm not found.";)

# xml parsing libxml2
AM_PATH_XML2(2.4.0)
AC_SUBST(XML_CPPFLAGS)
AC_SUBST(XML_LIBS)

# option to add here PACKAGE_LIBS = "$PACKAGE_LIBS -lextralib"
# gtk+ compilation will automatically add png and zlib
AC_SUBST(PACKAGE_LIBS)

# default to gtk+-2.0 setting to 1
GTK_HAVE_API_VERSION_2=1
GTK_HAVE_API_VERSION_3=0

AC_MSG_CHECKING([which gtk+ version to compile against])
AC_ARG_WITH([gtk],
  [AS_HELP_STRING([--with-gtk=2.0|3.0],[which gtk+ version to compile against (default: 2.0)])],
  [case "$with_gtk" in
     2.0|3.0) ;;
     *) AC_MSG_ERROR([invalid gtk version specified]) ;;
   esac],
  [with_gtk=2.0])
AC_MSG_RESULT([$with_gtk])

case "$with_gtk" in
  2.0) GTK_API_VERSION=2.0
       GTK_REQUIRED=2.0.0
       GTK_HAVE_API_VERSION_2=1
       GTK_HAVE_API_VERSION_3=0
       ;;
  3.0) GTK_API_VERSION=3.0
       GTK_REQUIRED=2.91.0
       GTK_HAVE_API_VERSION_2=0
       GTK_HAVE_API_VERSION_3=1
       ;;
esac

#
AC_SUBST([GTK_API_VERSION])
AC_SUBST([GTK_REQUIRED])

#
AM_CONDITIONAL([HAVE_GTK_2],[test "$with_gtk" = "2.0"])
AM_CONDITIONAL([HAVE_GTK_3],[test "$with_gtk" = "3.0"])

#
AC_DEFINE_UNQUOTED([GTK_HAVE_API_VERSION_2], [$GTK_HAVE_API_VERSION_2], [gtk version 2])
AC_DEFINE_UNQUOTED([GTK_HAVE_API_VERSION_3], [$GTK_HAVE_API_VERSION_3], [gtk version 3])

#
PKG_CHECK_MODULES(GTK, gtk+-$GTK_API_VERSION >= $GTK_REQUIRED)
AC_SUBST(GTK_CFLAGS)
AC_SUBST(GTK_LIBS)

# which lib to link in
AC_SUBST(PACKAGE_LIBS)


# final compiler settings
AC_SUBST(PACKAGE_CFLAGS)

# setting info
AC_MSG_RESULT([
Building programs with prefix=$prefix
  c-compiler:          $CC
  initial-c-flags:     $INITIAL_CFLAGS
  c-flags:             $CFLAGS
  warning-c-flags:     $WARNING_CFLAGS
  gtk-c-flags:         $GTK_CFLAGS
  gtk-libs:            $GTK_LIBS
  xml-c-flags:         $XML_CPPFLAGS
  xml-libs:            $XML_LIBS
]);

AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT
